package com.lzh.dlykserver.controller;

import com.github.pagehelper.PageInfo;
import com.lzh.dlykserver.entity.TDicType;
import com.lzh.dlykserver.query.DicTypeDTO;
import com.lzh.dlykserver.result.R;
import com.lzh.dlykserver.service.DicTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class DictypeController {

    private final DicTypeService dictypeService;

    /**
     * 获取字典类型
     * @param current
     * @param dicTypeDTO
     * @return
     */
    @GetMapping("/dict-types")
    public R getDicTypes(@RequestParam(value = "current", required = false) Integer current,
                         DicTypeDTO dicTypeDTO) {
        if (current == null) {
            current = 1;
        }
        PageInfo<TDicType> pageInfo = dictypeService.getDicTypes(current, dicTypeDTO);
        return R.OK(pageInfo);
    }

    /**
     * 获取字典类型详情
     * @param id
     * @return
     */
    @GetMapping("/dict-type/{id}")
    public R getDicTypeById(@PathVariable("id") Integer id) {
        TDicType dicType = dictypeService.getDicTypeById(id);
        return R.OK(dicType);
    }

    /**
     * 删除字典类型
     * @param id
     * @return
     */
    @DeleteMapping("/dict-type/{id}")
    public R deleteDicTypeById(@PathVariable("id") Integer id) {
        int i = dictypeService.deleteDicTypeById(id);
        return R.OK(i);
    }

    /**
     * 编辑字典类型
     * @param dicType
     * @return
     */
    @PutMapping("/dict-type/edit")
    public R editDicType(@RequestBody TDicType dicType) {
        int i = dictypeService.updateDicType(dicType);
        return i > 0 ? R.OK() : R.FAIL();
    }

    /**
     * 添加字典类型
     * @param dicType
     * @return
     */
    @PostMapping("/dict-type/add")
    public R addDicType(@RequestBody TDicType dicType) {
        int i = dictypeService.addDicType(dicType);
        return i > 0 ? R.OK() : R.FAIL();
    }

    /**
     * 批量删除字典类型
     * @param ids
     * @return
     */
    @DeleteMapping("/dict-types/batch-delete")
    public R batchDeleteDicType(@RequestParam("ids") String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        int i = dictypeService.batchDeleteDicType(idList);
        return i > 0 ? R.OK() : R.FAIL();
    }

}
